<template>
  <div class="app-container">
    <a-title @clickPreview="preview" />
    <el-form ref="queryForm" :model="queryParams" :inline="true" label-width="68px">
      <el-form-item label="文件名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入文件名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="更新时间" prop="fileUpdateTime">
        <el-date-picker
          v-model="dateRange"
          size="small"
          style="width: 220px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">查询</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-download"
          size="mini"
          :disabled="multiple"
          @click="bathDownload"
        >下 载</el-button>
      </el-col>
    </el-row>

    <el-table
      stripe
      v-loading="loading"
      :data="templateList"
      @selection-change="handleSelectionChange"
      @sort-change="tableSortChange"
      :default-sort="{prop: 'fileUpdateTime', order: 'descending'}"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        label="文件名称"
        align="center"
        prop="name"
        :sort-orders="['ascending', 'descending']"
        sortable="custom"
      />
      <el-table-column
        label="更新时间"
        align="center"
        sortable="custom"
        prop="fileUpdateTime"
        :sort-orders="['ascending', 'descending']"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.fileUpdateTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="文件大小"
        sortable="custom"
        align="center"
        prop="contentSize"
        :sort-orders="['ascending', 'descending']"
      />
      <el-table-column
        label="模板文件"
        align="center"
        :sort-orders="['ascending', 'descending']"
        sortable="custom"
        prop="fileName"
      >
        <template slot-scope="scope">
          <span>{{ scope.row.fileName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <!-- <a class="a-style" :href="scope.row.storagePath" :download="scope.row.fileName"  style="  cursor: pointer;"><i class="el-icon-download aicon"></i>下载</a> -->
          <a @click="downloadOne(scope.row.guid,scope.row.fileName)" class="a-style">
            <i class="el-icon-download aicon"></i>下载
          </a>
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-download"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['library:template:edit']"
          >下载</el-button>-->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-search"
            @click="handlePreview(scope.row)"
          >预览</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 预览 -->
    <el-dialog :visible.sync="openPreview" width="80%">
      <iframe :src="manualPreviewUrl" frameborder="0" width="100%" height="700px"></iframe>
    </el-dialog>
  </div>
</template>

<script>
import {
  listTemplate,
  getTemplate,
  delTemplate,
  addTemplate,
  updateTemplate,
  exportTemplate
} from "@/api/businessStandardLibrary/template/template";
import { handleBatchDownload } from '@/api/zipdownload';
import aTitle from "@/components/Title";
export default {
  name: "Template",
  components: { aTitle },
  data () {
    return {
      manualPreviewUrl: null,
      openPreview: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      files: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 模板下载表格数据
      templateList: [],
      // 查询参数
      dateRange: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: undefined,
        businessCategoryId: undefined,
        sortBy: undefined,
        order: undefined
      },
      // 表单参数
      form: {}
    };
  },
  created () {
    this.queryParams.businessCategoryId = this.$route.params.businessCategoryId;
    this.getList();
  },
  mounted(){
    document.title=document.getElementById("businessNameTitle").innerHTML;
  },
  methods: {
    preview () {
      this.openPreview = true;
      this.manualPreviewUrl = `${this.previewUrl}${encodeURIComponent(
        `${this.operationManual}业务主页/常用文件.xlsx`
      )}&name=${encodeURIComponent("常用文件.xlsx")}`;
    },
    /** 查询模板下载列表 */
    getList () {
      this.loading = true;
      listTemplate(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.templateList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel () {
      this.open = false;
      this.reset();
    },
    downloadOne (id, name) {
      this.downloadBlob(id, name)
    },
    // 表单重置
    reset () {
      this.form = {
        name: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.resetForm("queryForm");
      this.dateRange = [];
      this.handleQuery();
      this.getList();
    },
    // 多选框选中数据
    handleSelectionChange (selection) {
      this.files = selection
      this.ids = selection.map(item => item.id);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    bathDownload () {
      const filesList = []
      this.files.map(item => {
        filesList.push({
          fileurl: '/api/system/download/' + item.guid,
          fileName: item.fileName
        })
      })
      handleBatchDownload(filesList)
    },
    handlePreview (row) {
      this.filePreview(row.fileName, row.storagePath);
    },
    tableSortChange (column) {
      this.queryParams.pageNum = 1;

      if (column.order === "descending") {
        this.queryParams.sortBy = column.prop;
        this.queryParams.order = "desc";
      } else {
        this.queryParams.sortBy = column.prop;
        this.queryParams.order = "asc";
      }
      this.getList();
    }
  }
};
</script>
<style lang="scss" scoped>
.a-style {
  color: #409eff;
}
</style>